import {request, handleFetchResultNotification, REQUEST_TYPE} from '../utils';
import {formatMessage} from './intl';
import stringify from 'Shared/utils/stringify';
import {
    List,
    Detail,
    Categories,
    PWC,
    Brands,
    Assist,
    ProductCategoryList
} from './DTO';
const baseUrl = '/afterSales/api/v1';
import {baseDataStatus} from '../Enum';
import {PAGE} from '../constants';
const resourceName = formatMessage({
    id: 'api.resourceName',
    defaultMessage: '服务属性'
});

const listDesc = formatMessage({
    id: 'api.message.listDesc',
    defaultMessage: '查询{resourceName}'
}, {resourceName});

const detailDesc = formatMessage({
    id: 'api.message.detailDesc',
    defaultMessage: '查询{resourceName}详情'
}, {resourceName});

const updateDesc = formatMessage({
    id: 'api.message.updateDesc',
    defaultMessage: '编辑{resourceName}'
}, {resourceName});

const batchDesc = formatMessage({
    id: 'api.message.batchDesc',
    defaultMessage: '批量修改分类标准'
});

const resourceName2 = formatMessage({
    id: 'api.resourceName2',
    defaultMessage: '备件分类'
});

const categoryDesc = formatMessage({
    id: 'api.message.categoryDesc',
    defaultMessage: '查询{resourceName2}'
}, {resourceName2});

const addCategoryDesc = formatMessage({
    id: 'api.message.addCategoryDesc',
    defaultMessage: '新增{resourceName2}'
}, {resourceName2});

const updateCategoryDesc = formatMessage({
    id: 'api.message.updateCategoryDesc',
    defaultMessage: '编辑{resourceName2}'
}, {resourceName2});

const abandonCategoryDesc = formatMessage({
    id: 'api.message.abandonCategoryDesc',
    defaultMessage: '作废{resourceName2}'
}, {resourceName2});


const resourceName3 = formatMessage({
    id: 'api.resourceName3',
    defaultMessage: '自定义辅料'
});

const modeDesc = formatMessage({
    id: 'api.message.modeDesc',
    defaultMessage: '查询{resourceName3}'
}, {resourceName3});

const detailModeDesc = formatMessage({
    id: 'api.message.detailModeDesc',
    defaultMessage: '{resourceName3}详情'
}, {resourceName3});


const addModeDesc = formatMessage({
    id: 'api.message.addModeDesc',
    defaultMessage: '新增{resourceName3}'
}, {resourceName3});

const updateModeDesc = formatMessage({
    id: 'api.message.updateModeDesc',
    defaultMessage: '编辑{resourceName3}'
}, {resourceName3});


const pwcDesc = formatMessage({
    id: 'api.message.pwcDesc',
    defaultMessage: '查询备件索赔分类标准'
});
const brandDesc = formatMessage({
    id: 'api.message.brandDesc',
    defaultMessage: '查询品牌'
});

const partCategoryQueryDesc = formatMessage({
    id: 'api.message.partCategoryQueryDesc',
    defaultMessage: '查询备件分类'
});
const getDealerDesc = formatMessage({
    id: 'api.message.getDealerDesc',
    defaultMessage: '查询服务站'
});
const getProductCategoryListDesc = formatMessage({
    id: 'api.message.getProductCategoryListDesc',
    defaultMessage: '查询车型'
});
const importTempDesc = formatMessage({
    id: 'api.message.importTempDesc',
    defaultMessage: '下载修改分类标准导入模板'
});

export const getBrands = () => {
    const queryCondition = stringify({
        status: baseDataStatus.生效
    });
    return request(`${baseUrl}/brands?${queryCondition}`, null, Brands)
        .then(handleFetchResultNotification(REQUEST_TYPE.initQuery, brandDesc));
};
// 查询
export const getList = queryCondition => {
    const queryString = stringify(queryCondition);
    return request(`${baseUrl}/parts/forServiceProperty?${queryString}`, null, List)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, listDesc));
};

// 查询详情
export const getDetail = (id, isUpdate) => request(`${baseUrl}/parts/${id}/forServiceProperty?isUpdate=${isUpdate}`, null, Detail)
    .then(handleFetchResultNotification(REQUEST_TYPE.initQuery, detailDesc));

// 编辑
export const updateInfo = (id, data) => request(`${baseUrl}/parts/${id}/forServiceProperty`, {
    method: 'PUT',
    headers: {
        'Content-Type': 'application/json'
    },
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, updateDesc));

// 批量修改分类标准
export const updateBacth = data => request(`${baseUrl}/parts/batchCategory`, {
    method: 'PUT',
    headers: {
        'Content-Type': 'application/json'
    },
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, batchDesc));

// 查询顶层维修分类
export const getCategoriesTop = () => request(`${baseUrl}/partCategories/topLevel/forCurrentUser`, null, Categories)
    .then(handleFetchResultNotification(REQUEST_TYPE.query, categoryDesc));

// 查询维修分类
export const getCategories = id => request(`${baseUrl}/partCategories/${id}/children`, null, Categories)
    .then(handleFetchResultNotification(REQUEST_TYPE.query, categoryDesc));


// 新增维修分类
export const addCategory = data => request(`${baseUrl}/partCategories`, {
    method: 'POST',
    headers: {
        'Content-Type': 'application/json'
    },
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, addCategoryDesc));

// 编辑维修分类
export const updateCategory = (id, data) => request(`${baseUrl}/partCategories/${id}`, {
    method: 'PUT',
    headers: {
        'Content-Type': 'application/json'
    },
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, updateCategoryDesc));

// 作废维修分类
export const abandonCategory = (id, data) => request(`${baseUrl}/partCategories/${id}/abandon`, {
    method: 'PUT',
    headers: {
        'Content-Type': 'application/json'
    },
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, abandonCategoryDesc));

// 查询自定义辅料列表
export const getAssists = queryCondition => {
    const queryString = stringify(queryCondition);
    return request(`${baseUrl}/userDefinedAssistParts?${queryString}`, null, Assist)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, modeDesc));
};

// 新增自定义辅料
export const addAssist = data => request(`${baseUrl}/userDefinedAssistParts`, {
    method: 'POST',
    headers: {
        'Content-Type': 'application/json'
    },
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, addModeDesc));

// 编辑自定义辅料
export const updateAssist = (id, data) => request(`${baseUrl}/userDefinedAssistParts/${id}`, {
    method: 'PUT',
    headers: {
        'Content-Type': 'application/json'
    },
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, updateModeDesc));

// 自定义辅料详情
export const detailAssist = (id, data) =>
    request(`${baseUrl}/userDefinedAssistParts/${id}`).then(handleFetchResultNotification(REQUEST_TYPE.query, detailModeDesc));

// 查询产品分类
export const getPCSs = queryCondition => {
    const queryString = stringify({
        ...queryCondition,
        status: baseDataStatus.生效,
    });
    return request(`${baseUrl}/partWarrantyCategories?${queryString}`, null, PWC)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, pwcDesc));
};

export const getPCSsimple = queryCondition => {
    const queryString = stringify({
        ...queryCondition,
        status: baseDataStatus.生效,
        pageSize: PAGE.smallSize,
        pageIndex: PAGE.index,
    });
    return request(`${baseUrl}/partWarrantyCategories?${queryString}`, null, PWC)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, pwcDesc)).then(res => {
            if(res.ok) return res.data.content;
            return false;
        });
};

//备件分类查询
export const partCategoryQuery = queryCondition => {
    const queryString = stringify(queryCondition);
    return request(`${baseUrl}/partCategories/forUserDefinedAssistPart?${queryString}`, null, null)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, partCategoryQueryDesc));
};
// 查询服务站
export const getDealers = pageOption => {
    const queryString = stringify(pageOption);
    return request(`${baseUrl}/dealers/forServiceInformation?${queryString}`, null, null)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getDealerDesc));
};
// 查询车型
export const getProductCategory = pageOption => {
    const queryString = stringify(pageOption);
    return request(`${baseUrl}/productCategories/forExceptionInfoTransfer?${queryString}`, null, ProductCategoryList)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getProductCategoryListDesc));
};

// 导入工作日历模板
export const importTemp = () => request(`${baseUrl}/parts/forServiceProperty/importTemp`)
    .then(handleFetchResultNotification(REQUEST_TYPE.submit, importTempDesc)).then(res => res.data);

